package com.ruoyi.web.controller.social.program;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Program;
import com.ruoyi.common.core.domain.entity.ProgramRecord;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ProgramRecordService;
import com.ruoyi.system.service.ProgramService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/social/program/programRecord")
public class ProgramRecordController extends BaseController {
    @Resource
    private ProgramRecordService programRecordService;

    @Resource
    private ProgramService programService;

    @Log(title = "项目进度记录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProgramRecord programRecord) {
        List<ProgramRecord> list = programRecordService.selectAll(programRecord);
        ExcelUtil<ProgramRecord> util = new ExcelUtil<ProgramRecord>(ProgramRecord.class);
        util.exportExcel(response, list, "项目进度记录");
    }

    @Log(title = "获取项目进度记录", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(ProgramRecord programRecord) {
        startPage();
        List<ProgramRecord> list = programRecordService.selectProgramRecordList(programRecord);
        return getDataTable(list);
    }

    @Log(title = "获取所有项目进度记录", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProgramRecord programRecord) {
        List<ProgramRecord> list = programRecordService.selectAll(programRecord);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(programRecordService.selectById(id));
    }

    @Log(title = "添加项目进度记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@Validated @RequestBody ProgramRecord programRecord) {
        programRecord.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = programRecordService.createProgramRecord(programRecord);

        Program search = programService.selectById(programRecord.getProgramId());
        if (search == null || !search.getCreateBy().equals(SecurityUtils.getLoginUser().getUser().getNickName())) {
            throw new BaseException("该单据由" + search.getCreateBy() + "创建，您无法修改");
        }

        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改项目进度记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@Validated @RequestBody ProgramRecord programRecord) {
        programRecord.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = programRecordService.updateProgramRecord(programRecord);

        ProgramRecord programRecord1  = programRecordService.selectById(programRecord.getId());

        Program search = programService.selectById(programRecord1.getProgramId());
        if (search == null || !search.getCreateBy().equals(SecurityUtils.getLoginUser().getUser().getNickName())) {
            throw new BaseException("该单据由" + search.getCreateBy() + "创建，您无法修改");
        }

        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除项目进度记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long id) {
        ProgramRecord programRecord1  = programRecordService.selectById(id);

        Program search = programService.selectById(programRecord1.getProgramId());
        if (search == null || !search.getCreateBy().equals(SecurityUtils.getLoginUser().getUser().getNickName())) {
            throw new BaseException("该单据由" + search.getCreateBy() + "创建，您无法删除");
        }

        return toAjax(programRecordService.deleteById(id));
    }
}
